var storefrontList = [];
var deliveryManList = [];

$("#dop_datagrid_area_relate").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : true,
	fit : false,
	pageSize : 10,
	pageList : [10, 30, 50, 100],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [[]],
	columns : [[{
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "baseAreaName",
		width : 150,
		title : "区域名称"
	}, {
		field : "baseEnable",
		width : 30,
		title : "启用"
	}, {
		field : "deliverymanId",
		width : 150,
		title : "已关联人员ID",
		hidden : true
	}, {
		field : "deliverymanName",
		width : 500,
		title : "已关联人员名称"
	},{
		field : "storefrontId",
		width : 500,
		title : "已关联店面ID",
		hidden : true
	}, {
		field : "storefrontName",
		width : 500,
		title : "已关联店面名称"
	}
	]]
});



// init
function init() {
	// init
	var options = $("#dop_datagrid_area_relate").datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("areaController.do?queryList", params, true,
			function(data) {
				$("#dop_datagrid_area_relate").datagrid('loadData', {
					"rows" : data.list,
					"total" : data.count
				} || {});
				var p = $("#dop_datagrid_area_relate").datagrid('getPager');
				$("td:last", p).find("a").click();
			});
	// click pagination
	var p = $("#dop_datagrid_area_relate").datagrid('getPager');
	$(p).pagination(
			{
				onSelectPage : function(pageNumber, pageSize) {
					var params = $.extend({
						pageNum : pageNumber,
						pageSize : pageSize
					}, getQueryParams());
					EapTools.doAction("areaController.do?queryList", params,
							false, function(data) {
								$("#dop_datagrid_area_relate").datagrid('loadData',
										{
											"rows" : data.list,
											"total" : data.count
										} || {});
							});
				}
			});
		
}

$(function(){
	init();
});

// bind assign event
$("#dop_btn_area_assign")
		.click(
				function() {
					var selections = $("#dop_datagrid_area_relate").datagrid(
							"getSelections");
					if (selections && selections.length > 0) {
						if (selections.length > 1) {
							layer.tips('请选择一行数据', this, {
								guide : 2,
								time : 2
							});
						} else {
							var storefrontId = $("#storefront").val();
							var deliverymanId = $("#deliveryman").val();
							EapTools
									.doAction(
											"areaRelateController.do?assignStorefrontAndDeliveryman",
											{
												id : $(
														"#dop_datagrid_area_relate")
														.datagrid(
																"getSelections")[0].id,
												storefrontIdList : storefrontId,
												deliverymanIdList : deliverymanId
											}, true, function(data) {
												if (data.success) {
													EapTip.msgOk("分配成功");
												} else {
													EapTip.msgError("分配失败");
												}
												init();
											});
						}
					} else
						layer.tips('请选择要修改行', this, {
							guide : 2,
							time : 2
						});
				});

// bind query event
$("#dop_btn_area_query").click(function() {
	init();
});

// get params
function getQueryParams() {
	return {
		baseAreaName : $("input[code='baseAreaName']").val()
	};
}



//行切换
$("#dop_datagrid_area_relate")
		.datagrid(
				{
					onClickRow : function() {
						var selections = $("#dop_datagrid_area_relate")
								.datagrid("getSelections");
						var areaId = selections[0].id;

						// 查询店面信息
						EapTools.doAction("storefrontController.do?findData", {}, false, function(
								data) {
							storefrontList=data;
						});
						// 查询业务人员信息
						EapTools.doAction("deliveryManController.do?findData",
								{}, false, function(data) {
									deliveryManList = data;
						});

						$("#storefront").empty();
						$("#deliveryman").empty();

						// 查询店面信息
						EapTools
								.doAction(
										"areaController.do?getById",
										{
											id : areaId
										},
										false,
										function(data) {
											var selectedStorefrontIdList = data.storefrontIdList;
											// 区域
											for (var i = 0; i < storefrontList.length; i++) {
												//如果选中的区域ID为空，则说明之前用户没有选中任何数据，则显示所有
												if (selectedStorefrontIdList == "undefined"
														|| selectedStorefrontIdList.length == 0) {
													$("#storefront")
															.append(
																	"<option value='"
																			+ storefrontList[i].id
																			+ "'>"
																			+ storefrontList[i].stoName
																			+ "</option>");
												}

												for (var j = 0; j < selectedStorefrontIdList.length; j++) {
													if (selectedStorefrontIdList[j] === storefrontList[i].id) {
														$("#storefront")
																.append(
																		"<option value='"
																				+ storefrontList[i].id
																				+ "'  selected='selected'>"
																				+ storefrontList[i].stoName
																				+ "</option>");
														break;
													}
													// 最后一个还是不等，则说明没有勾选
													if (j === selectedStorefrontIdList.length - 1) {
														$("#storefront")
																.append(
																		"<option value='"
																				+ storefrontList[i].id
																				+ "'>"
																				+ storefrontList[i].stoName
																				+ "</option>");
													}
												}
											}
											// 员工姓名
											var selectedDeliveryManIdList = data.deliveryManIdList;
											for (var i = 0; i < deliveryManList.length; i++) {
												if (selectedDeliveryManIdList == "undefined"
														|| selectedDeliveryManIdList.length == 0) {
													$("#deliveryman")
															.append(
																	"<option value='"
																			+ deliveryManList[i].id
																			+ "'>"
																			+ deliveryManList[i].delName
																			+ "</option>");
												}

												for (var j = 0; j < selectedDeliveryManIdList.length; j++) {
													if (selectedDeliveryManIdList[j] === deliveryManList[i].id) {
														$("#deliveryman")
																.append(
																		"<option value='"
																				+ deliveryManList[i].id
																				+ "'  selected='selected'>"
																				+ deliveryManList[i].delName
																				+ "</option>");
														break;
													}
													// 最后一个还是不等，则说明没有勾选
													if (j === selectedDeliveryManIdList.length - 1) {
														$("#deliveryman")
																.append(
																		"<option value='"
																				+ deliveryManList[i].id
																				+ "'>"
																				+ deliveryManList[i].delName
																				+ "</option>");
													}
												}
											}
										});
						$("#storefront").multipleSelect({
							filter : true
						});
						$("#deliveryman").multipleSelect({
							filter : true
						});
					}
				});

